<?php

namespace app\admin\controller\users;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 健康档案展示
 *
 * @icon fa fa-user
 */
class Tab extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Info;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {
        $ids = request()->param()['ids'];

        if (isset(request()->param()['page'])){
            $pan_in = request()->param()['page'];
        }else{
            $pan_in = 0;
        }


        //$this->loadlang('general/attachment');
        //$this->loadlang('general/crontab');
        $row = $this->model->get(['id' => $ids]);

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $vip = Db::table('fa_users')
            ->alias('a')
            ->field('b.level_name,c.name')
            ->join('fa_vip_level b','a.level=b.id','left')
            ->join('fa_vip_card c','a.vip=c.id','left')
            ->where('a.id',$row->user_id)
            ->find();

        $arr = Db::table('fa_diagnosis_order')
            ->where('info_id',$ids)
            ->select();

        if (!empty($arr)){
            foreach ($arr as $k=>$v){
                $arr[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $arr[$k]['diagnosiss'] = Db::table('fa_diagnosiss')
                    ->where('order_id',$v['id'])
                    ->select();

                $arr[$k]['drug'] = Db::table('fa_drug')
                    ->where('order_id',$v['id'])
                    ->select();

                $arr[$k]['vip'] = Db::table('fa_serve')
                    ->where('order_id',$v['id'])
                    ->select();
            }
        }

        $data = Db::table('fa_report')
            ->where('order_id',$ids)
            ->select();

        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['type_name'] = '';

                if ($v['type'] == 1){
                    $data[$k]['type_name'] = '健康顾问';

                    $salesman_arr = Db::table('fa_admin')
                        ->where('id',$v['admin_id'])
                        ->find();

                    if (empty($salesman_arr)){
                        $data[$k]['salesman_name'] = '';
                    }else{
                        $data[$k]['salesman_name'] = $salesman_arr['nickname'];
                    }
                }else{
                    $salesman_arr = Db::table('fa_medic')
                        ->where('id',$v['salesman_id'])
                        ->find();

                    if (empty($salesman_arr)){
                        $data[$k]['salesman_name'] = '';
                    }else{

                        $data[$k]['type_name'] = $salesman_arr['type'];
                        $data[$k]['salesman_name'] = $salesman_arr['name'];
                    }
                }

                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

                $logs_arr = Db::table('fa_report_log')
                    ->field('id,title,check_time,image,video,file')
                    ->where('report_id',$v['id'])
                    ->select();

                $image_arr = [];
                $images_arr = [];

                $video_arr = [];
                $videos_arr = [];

                $file_arr = [];
                $files_arr = [];

                $i = 0;
                if (!empty($logs_arr)){
                    foreach ($logs_arr as $ks=>$vs){
                        $i += 100;
                        $image_arr = explode(';',$vs['image']);
                        if (!empty($image_arr)){
                            foreach ($image_arr as $ki=>$vi){
                                if (!empty($vi)){
                                    $images_arr[$ki+1000+$i]['image'] = $vi;
                                    $images_arr[$ki+1000+$i]['title'] = $vs['title'];
                                    $images_arr[$ki+1000+$i]['check_time'] = $vs['check_time'];
                                }
                            }
                        }

                        $video_arr = explode(';',$vs['video']);

                        if (!empty($video_arr)){
                            foreach ($video_arr as $ki=>$vi){
                                if (!empty($vi)){
                                    $videos_arr[$ki+1000+$i]['video'] = $vi;
                                    $videos_arr[$ki+1000+$i]['title'] = $vs['title'];
                                    $videos_arr[$ki+1000+$i]['check_time'] = $vs['check_time'];
                                    $videos_arr[$ki+1000+$i]['suffix'] = substr(strrchr($vi, '.'), 1);
                                }
                            }
                        }

                        $file_arr = explode(';',$vs['file']);
                        if (!empty($file_arr)){
                            foreach ($file_arr as $ki=>$vi){
                                if (!empty($vi)){
                                    $files_arr[$ki+1000+$i]['file'] = $vi;
                                    $files_arr[$ki+1000+$i]['title'] = $vs['title'];
                                    $files_arr[$ki+1000+$i]['check_time'] = $vs['check_time'];
                                    $files_arr[$ki+1000+$i]['suffix'] = substr(strrchr($vi, '.'), 1);
                                }
                            }
                        }

                    }

                    $data[$k]['image'] = array_merge($images_arr);
                    $data[$k]['video'] = array_merge($videos_arr);
                    $data[$k]['file'] = array_merge($files_arr);
                }

            }
        }

        //var_dump($data);die();

        $rows = Db::table('fa_life')
            ->where('info_id',$ids)
            ->select();

        if (!empty($rows)){
            foreach ($rows as $k=>$v){
                $rows[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }



        $vip_names = empty($vip['name']) ? $vip['level_name']:$vip['level_name'].'('.$vip['name'].')';


        //var_dump(request()->param());
        $log = Db::table('fa_log')
            ->where('info_id',$ids)
            ->paginate(10);

        // 获取分页显示
        $page = $log->render();
        $log = $log->all(); //这是关键

        //var_dump($log);die();
        if (!empty($log)){
            foreach ($log as $k=>$v){
                $log[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $log[$k]['type_name'] = '';
                $log[$k]['names'] = '';
                $log[$k]['images'] = [];

                if ($v['type'] == 1){
                    $type_name = Db::table('fa_admin')
                        ->field('id,nickname')
                        ->where('id',$v['salesman_id'])
                        ->find();
                    if (!empty($type_name)){
                        $log[$k]['names'] = $type_name['nickname'];
                    }
                    $log[$k]['type_name'] = '健康顾问';
                }else{
                    $type_name = Db::table('fa_medic')
                        ->field('id,name,type')
                        ->where('id',$v['admin_id'])
                        ->find();
                    if (!empty($type_name)){
                        $log[$k]['names'] = $type_name['name'];
                        $log[$k]['type_name'] = $type_name['type'];
                    }
                }

                $image = explode(';',$v['image']);
                if (!empty($image)){
                    foreach ($image as $kk=>$vv){
                        $suffix = substr(strrchr($vv, '.'), 1);
                        $log[$k]['images'][$kk]['name'] = $vv;
                        $log[$k]['images'][$kk]['suffix'] = $suffix;
                    }

                }


            }

        }

        //var_dump($log);die();


        $row->age = countage($row->birthday);
        $this->view->assign("data", $data);
        $this->view->assign("arr", $arr);
        $this->view->assign("row", $row);
        $this->view->assign("rows", $rows);
        $this->view->assign("vip", $vip_names);
        $this->view->assign("log", $log);
        $this->view->assign("page", $page);
        $this->view->assign("pan_in", $pan_in);

        return $this->view->fetch();
    }

    public function table1()
    {
        $this->model = new \app\admin\model\Info;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(["users"=>['id','level']])
                ->where($where)
                ->order('id', 'desc')
                ->paginate($limit);

            if (!empty($list->items())){
                foreach ($list->items() as $k=>$v){

                    $ni = Db::table('fa_vip_level')
                        ->where('id',$v->users->level)
                        ->find();

                    if (empty($ni)){
                        $list->items()[$k]->users->level = '';
                    }else{
                        $list->items()[$k]->users->level = $ni['level_name'];
                    }
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch('index');
    }

    public function table2()
    {
        $this->model = model('AdminLog');
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch('index');
    }


}
